/*
 * @(#)PortViewServer.java   09/09/13
 *
 * Copyright (C) 2009 Ecole des Mines d'Ales
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: yulinz88@gmail.com
 *
 *
 *
 */



package fr.ema.arch3d.gea.port;

import fr.ema.arch3d.gea.Evolution;

import org.jgraph.graph.CellViewRenderer;
import org.jgraph.graph.PortRenderer;
import org.jgraph.graph.PortView;

import java.awt.Graphics;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

import javax.swing.ImageIcon;

/**
 * Class description
 *
 *
 * @version        1.0, 09/09/13
 * @author         Huaxi (Yulin) ZHANG
 */
public class PortViewServer extends PortView {

    /** Field description */
    protected static ImageIcon portIcon = null;

    /** Field description */
    protected static MyPortRenderer renderer = new MyPortRenderer();

    /**
     * Constructs ...
     *
     *
     * @param p
     */
    public PortViewServer(MyPort p) {
        super(p);
        portIcon = createImageIcon("resource/icons/starPink.png", "server");
    }

    /**
     * Returns the bounds for the port view.
     */
    public Rectangle2D getBounds() {
        if (portIcon != null) {
            Point2D     pt     = (Point2D) getLocation().clone();
            int         width  = portIcon.getIconWidth();
            int         height = portIcon.getIconHeight();
            Rectangle2D bounds = new Rectangle2D.Double();

            bounds.setFrame(pt.getX() - width / 2, pt.getY() - height / 2, width, height);

            return bounds;
        }

        return super.getBounds();
    }

    public CellViewRenderer getRenderer() {
        return renderer;
    }

    /** Returns an ImageIcon, or null if the path was invalid. */
    protected static ImageIcon createImageIcon(String path, String type) {
        java.net.URL imgURL = Evolution.class.getResource(path);

        if (imgURL != null) {
            return new ImageIcon(imgURL, type);
        } else {
            System.err.println("Couldn't find file: " + path);

            return null;
        }
    }

    /**
     * Class description
     *
     *
     * @version        1.0, 09/09/13
     * @author         Huaxi (Yulin) ZHANG
     */
    public static class MyPortRenderer extends PortRenderer {
        public void paint(Graphics g) {
            portIcon.paintIcon(this, g, 0, 0);
        }
    }
}
